import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AuthService } from "./auth.service";

/**
 * @ 需要根据不同的登录类型(学生,教师,管理员)设置不同的守卫
 */
@Injectable()
export class AuthGuard implements CanActivate {

  constructor(public Auth: AuthService, private router: Router) {

  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    // 没有token未登录 跳转到登录页
    if (!this.Auth.token.token) {
      this.router.navigate(['/login']);
    }

    return !!this.Auth.token;

  }
}
